#!/bin/bash

data_path=/app/dmpdata
start_date=$1
username=zxcms_e
password=zxcms
split=0x01

if [ -z "${start_date}" ]; then
  echo "Example: load2txt.sh 20210827"
  exit
fi

get_current_time() {
  echo `date "+%Y-%m-%d %H:%M:%S"`
}

echo "Start load data to txt file at: `get_current_time`"

if [ ! -d "$data_path/$start_date" ]; then
  mkdir -p "$data_path/$start_date"
fi

table_name_cnfs=$(cat ./load2txt.cnf)

for cnf in $table_name_cnfs
do
  table_name=${cnf/_TIME_WINDOW_ALL/}
  table_name=${table_name/_TIME_WINDOW_INC/}

  file_path=$data_path/$start_date/${cnf/TIME_WINDOW/$start_date}.txt

  echo "`get_current_time` Starting load table:$table_name to txt file."

  sqluldr user="${username}/${password}@10.18.37.35:1521/hnzx" \
    query="select * from $table_name;" \
    head="no" \
    file="$file_path" \
    charset="utf-8"
    safe="true"
    field="0x01"

  echo "`get_current_time` Finished load table:$table_name to txt file."
done
  
echo "`get_current_time` Finished load data to txt file."

